<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>发送和接收json格式的数据</title>
    <script src="jquery-1.11.1.js" type="text/javascript"></script>
</head>
<body>

        <button id="btn">点击发送ajax请求</button>
        <button id="btn2">点击发送ajax请求携带json格式数据</button>
        <script>
            $(function () {
                // btn
                $("#btn").click(function () {

                    $.ajax({
                        url:'/mvc/json/j1',
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            console.log(res)
                        }
                    })
                })

                // btn2
                $("#btn2").click(function () {
                    let  user = {
                        username:'jack',
                        password:'6666',
                        age:20
                    }
                    $.ajax({
                        url:'/mvc/json/j2',
                        data:JSON.stringify(user),
                        contentType:'application/json;charset=utf-8',  // ajax 携带的数据类型是json格式
                        dataType:'json',
                        type:'post',
                        success:function (res) {
                            console.log(res)
                        }
                    })
                })
            })
        </script>
</body>
</html>
